﻿using System;
using System.Collections.Generic;
using System.Management;

namespace DreamCube.Foundation.WMI
{
    public class HardDisk : ManagementClassBasic<HardDiskModel>
    {
        public override String ClassName
        {
            get { return ""; }
        }

        public override List<HardDiskModel> GetModels()
        {
            ManagementObjectSearcher hdSearch = new ManagementObjectSearcher("select * from Win32_DiskDrive");
            List<HardDiskModel> hdModels = new List<HardDiskModel>();
            String partitionSqlTemplate = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='{0}'} WHERE AssocClass = Win32_DiskDriveToDiskPartition";
            foreach (ManagementObject drive in hdSearch.Get())
            {
                HardDiskModel hdModel = new HardDiskModel();
                SetModelValue(hdModel, drive);
                ManagementObjectSearcher partitionSearch =
                    new ManagementObjectSearcher(partitionSqlTemplate.Replace("{0}", drive["DeviceID"].ToString()));
                foreach (ManagementObject partition in partitionSearch.Get())
                {
                    HardDiskPartitionModel partitionModel = new HardDiskPartitionModel();
                    SetModelValue(partitionModel, partition);
                    hdModel.Partitions.Add(partitionModel);
                }
                hdModels.Add(hdModel);
            }
            return hdModels;
        }
    }
}
